const FileService = require('../services/file.service');
const UserService = require('../services/users.service');
const {APP_PORT,APP_HOST} = require('../app/config');
class FileController {
  async savaAvatar(ctx, next) {
    const file = ctx.req.file
    const {filename, mimetype, size} = file
    const {id} = ctx.user
    //将文件信息存入数据库
    const result = await FileService.createAvatar(filename ,mimetype,size,id)
    const avatarUrl = `${APP_HOST}:${APP_PORT}/users/${id}/avatar`
    //更新用户头像信息
    await UserService.updateAvatarById(id, avatarUrl)
    ctx.body = {
      code: 200,
      msg: '上传成功',
      data: {
        filename,
        mimetype,
        size,
      }
    }
  }
  async savePicture(ctx, next) {
    const files = ctx.req.files
    const momentId = ctx.query.momentId
    const {id} = ctx.user
    for(let file of files) {
      const {filename, mimetype, size} = file
      //将文件信息存入数据库
      const result = await FileService.createPicture(filename, mimetype, size,momentId, id)
    }
    ctx.body = {
      code: 200,
      msg: '上传成功',
    }
  }
}
module.exports =new FileController();